package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.WorkOvertimeSpecMapper;
import com.eastfair.venueservice.dto.WorkOvertimeSpecDTO;
import com.eastfair.venueservice.entity.WorkOvertimeSpec;
import com.eastfair.venueservice.service.WorkOvertimeSpecService;
import com.eastfair.venueservice.vo.WorkOvertimeSpecVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 加班规格
 * </p>
 *
 * @author dqq
 * @date 2023-05-05
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class WorkOvertimeSpecServiceImpl extends SuperServiceImpl<WorkOvertimeSpecMapper, WorkOvertimeSpec> implements WorkOvertimeSpecService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<WorkOvertimeSpec> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(WorkOvertimeSpec model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<WorkOvertimeSpecVO> listVOByWorkOvertimeSpec(Long workOvertimeRuleId) {
        log.info("listVOByWorkOvertimeSpec - 加班规则ID查询规格列表VO, workOvertimeRuleId={}", workOvertimeRuleId);
        List<WorkOvertimeSpec> list = listByWorkOvertimeRuleId(workOvertimeRuleId);
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        return list
                .stream()
                .map(workOvertimeSpec -> BeanUtil.toBean(workOvertimeSpec, WorkOvertimeSpecVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<WorkOvertimeSpec> listWorkOvertimeSpec(WorkOvertimeSpecDTO query) {
        QueryWrapper<WorkOvertimeSpec> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(WorkOvertimeSpec::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(query.getWorkOvertimeRuleId() != null, WorkOvertimeSpec::getWorkOvertimeRuleId, query.getWorkOvertimeRuleId())
                ;
        return list(queryWrapper);
    }

    @Override
    public List<WorkOvertimeSpec> listByWorkOvertimeRuleId(Long workOvertimeRuleId) {
        WorkOvertimeSpecDTO query = new WorkOvertimeSpecDTO();
        query.setWorkOvertimeRuleId(workOvertimeRuleId);
        return listWorkOvertimeSpec(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkOvertimeSpec saveWorkOvertimeSpec(WorkOvertimeSpecDTO workOvertimeSpecDTO) {
        WorkOvertimeSpec workOvertimeSpec = BeanUtil.toBean(workOvertimeSpecDTO, WorkOvertimeSpec.class);
        save(workOvertimeSpec);
        return workOvertimeSpec;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<WorkOvertimeSpecDTO> list) {
        log.info("saveBatch - 批量保存加班规格, list={}", list);
        if (list == null || list.isEmpty()) {
            return false;
        }
        // 移除加班规则下的加班规格
        WorkOvertimeSpecDTO workOvertimeSpecDTO = list.get(0);
        removeByWorkOvertimeRuleId(workOvertimeSpecDTO.getWorkOvertimeRuleId());
        // 批量保存加班规格
        list.forEach(this::saveWorkOvertimeSpec);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<WorkOvertimeSpecDTO> list, Long workOvertimeRuleId) {
        log.info("saveBatch - 批量保存加班规格, list={}, workOvertimeRuleId={}", list, workOvertimeRuleId);
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(workOvertimeSpecDTO -> workOvertimeSpecDTO.setWorkOvertimeRuleId(workOvertimeRuleId));
        return saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkOvertimeSpec updateWorkOvertimeSpec(WorkOvertimeSpecDTO workOvertimeSpecDTO) {
        WorkOvertimeSpec workOvertimeSpec = BeanUtil.toBean(workOvertimeSpecDTO, WorkOvertimeSpec.class);
        updateById(workOvertimeSpec);
        return workOvertimeSpec;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByWorkOvertimeRuleId(Long ruleId) {
        log.info("removeByWorkOvertimeRuleId - 加班规则ID删除加班规格, ruleId={}", ruleId);
        List<WorkOvertimeSpec> list = listByWorkOvertimeRuleId(ruleId);
        if (list == null || list.isEmpty()) {
            return false;
        }
        return removeByIdsOfLogic(list);
    }
}
